#include "vlmodel.h"

VLModel::VLModel()
{
    readIniFile();
    workingSourceIndex = 0;
}

VLModel::~VLModel()
{
    writeIniFile();
}

int VLModel::readIniFile()
{
    QSettings settings(INI_FILE_LOCATION, QSettings::IniFormat);
    if(settings.status() == QSettings::NoError)
    {
        verbose = settings.value("verbose", false).toBool();
        gMotionThreshold = settings.value("global_motion_threshold", 20).toInt();
        gTimeSwitch = settings.value("global_time_switch", 2).toInt();
        QStringList groups = settings.childGroups();

        VideoList().clear();
        foreach (QString group, groups) {
            VideoSource videoSource;
            settings.beginGroup(group);
            videoSource.sourceName = group;
            videoSource.sourceType = (eVideoSourceType) settings.value("source_type", File).toInt();
            videoSource.source = settings.value("source", "").toString();
            videoSource.motionThreshold = settings.value("motion_threshold", 20).toInt();
            videoSource.timeSwitch = settings.value("time_switch", 2).toInt();
            videoSource.ROI = string2PointList(settings.value("ROI", NULL).toString());
            settings.endGroup();

            videoList.append(videoSource);
        }

        return 0;
    }else
        return -1; // access error or malformat
}

bool VLModel::writeIniFile()
{
    QSettings settings(INI_FILE_LOCATION, QSettings::IniFormat);
    settings.setValue("verbose", verbose);
    settings.setValue("global_motion_threshold", gMotionThreshold);
    settings.setValue("global_time_switch", gTimeSwitch);


    for(int i =0; i < videoList.count(); i++ )
    {
        settings.beginGroup(videoList[i].sourceName);
        settings.setValue("source_type", videoList[i].sourceType);
        settings.setValue("source", videoList[i].source);
        settings.setValue("ROI", pointList2String(videoList[i].ROI));
        settings.setValue("motion_threshold", videoList[i].motionThreshold);
        settings.setValue("time_switch", videoList[i].timeSwitch);
        settings.endGroup();
    }

    return true;
}

void VLModel::setGlobalSettings(bool _verbose, int _gMotionThreshold, int _gTimeSwitch)
{
    verbose = _verbose;
    gMotionThreshold = _gMotionThreshold;
    gTimeSwitch = _gTimeSwitch;
}

void VLModel::addVideoSource(VideoSource _videoSource)
{
    videoList.append(_videoSource);
}

int VLModel::removeVideoSource(int _videoSourceIndex)
{
    if(_videoSourceIndex >= 0 && _videoSourceIndex < videoList.count())
    {
        videoList.removeAt(_videoSourceIndex);
        return _videoSourceIndex;
    }else
        return -1;
}

void VLModel::clearVideoSource()
{
    videoList.clear();
}

VideoSource VLModel::getVideoSource(int sourceIndex)
{
    VideoSource video;
    if(sourceIndex > videoList.count() - 1 || sourceIndex < 0 )
        video = videoList[workingSourceIndex]; //invalid index, return the default source
    else{
        workingSourceIndex = sourceIndex;
        video = videoList[sourceIndex];
    }

    if(video.motionThreshold == 0)
        video.motionThreshold = gMotionThreshold;
    if(video.timeSwitch == 0)
        video.timeSwitch = gTimeSwitch;

    return video;
}

void VLModel::setROI(QList<CvPoint> list)
{
    videoList[workingSourceIndex].ROI = list;
}

void VLModel::clearROI()
{
    videoList[workingSourceIndex].ROI.clear();
}

QString VLModel::pointList2String(QList<CvPoint> pointList)
{
    QString strOfPoints;

    if(pointList.isEmpty())
        return QString::null;

    foreach (CvPoint point, pointList) {
        strOfPoints.append(QString("%1,%2,").arg(point.x).arg(point.y));
    }

    return strOfPoints;
}

QList<CvPoint> VLModel::string2PointList(QString strOfPoints)
{
    QList<CvPoint> pointList;
    CvPoint point;
    QStringList strList = strOfPoints.split(",",QString::SkipEmptyParts);

    pointList.empty();
    if(strList.count()%2 == 0 && strList.count() > 0)
    {
        for(int i =0; i < strList.count(); i+=2)
        {
            point.x = strList.at(i).toInt();
            point.y = strList.at(i+1).toInt();
            pointList.append(point);
        }
    }

    return pointList;
}
